//
//  XMGEssenceViewController.m
//  百思不得解国内
//
//  Created by sunnyday on 16/5/16.
//  Copyright © 2016年 sunnyday. All rights reserved.
//

#import "XMGEssenceViewController.h"
#import "allTableViewController.h"
#import "videoTableViewController.h"
#import "voiceTableViewController.h"
#import "picTableViewController.h"
#import "topicTableViewController.h"

@interface XMGEssenceViewController ()<UIScrollViewDelegate>

/**
 标签栏底部红色的指示器
 */
@property (nonatomic,strong)UIView * indicatorView;
/**
 当前选中的按钮
 */
@property (nonatomic,strong)UIButton * selectButton;
/**
 顶部的所有标签
 */
@property (nonatomic,strong)UIView * titleView;
/**
 底部的所有标签
 */
@property (nonatomic,strong)UIScrollView * contentView;

@end

@implementation XMGEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏
    [self setUpNav];
    // 初始化子控制器
    [self setUpChildVC];
    
    
    
    //设置顶部标签栏
    [self setupTitlesView];
    // 设置 navigationitem  的 title
    [self setUpContentView];
}
// 初始化子控制器
- (void)setUpChildVC
{
    allTableViewController *all = [[allTableViewController alloc] init];
    [self addChildViewController:all];
    videoTableViewController *video = [[videoTableViewController alloc]init];
    [self addChildViewController:video];
    voiceTableViewController *voice = [[voiceTableViewController alloc] init];
    [self addChildViewController:voice];
    picTableViewController *pic = [[picTableViewController alloc] init];
    [self addChildViewController:pic];
    topicTableViewController *topic = [[topicTableViewController alloc] init];
    [self addChildViewController:topic];
}
  // 设置导航栏
- (void)setUpNav
{
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" highImage:@"MainTagSubIconClick" target:self action:@selector(tagClick)];
    self.view.backgroundColor = XMGGlobalBg;
}
- (void)tagClick
{
    XMGLogFunc;
}

//设置顶部标签栏
- (void)setupTitlesView
{
    UIView *titlesView = [[UIView alloc]initWithFrame:CGRectMake(0, TitlesViewY, self.view.width, TitlesViewH)];
    titlesView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.7];
    [self.view addSubview:titlesView];
    self.titleView = titlesView;
    
    //底部的红色指示器
    
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor redColor];
    indicatorView.width = self.view.width;
    indicatorView.height = 2;
    // 此处 设置 iindicatorview 的tag 值  为了避免与button 冲突
    indicatorView.tag = -1;
    indicatorView.y = titlesView.height - indicatorView.height;
    self.indicatorView = indicatorView;

    
    // titlesView 上的子标签
    NSArray *titles = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
    CGFloat width = titlesView.width/ titles.count;
    CGFloat height = titlesView.height;
    for (NSInteger i = 0; i < titles.count; i ++) {
        UIButton *button = [[UIButton alloc]init];
        
        button.tag = i;
        
        button.height = height;
        button.width = width;
        button.x = i * width;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:button];
        
                //默认点击了一个按钮
        if (i == 0) {
            button.enabled = NO;
            self.selectButton = button;
            
            [button.titleLabel sizeToFit];
            self.indicatorView.width = button.titleLabel.width;
            self.indicatorView.centerX = button.centerX;
            
        }
        
        
    }
    self.titleView = titlesView;
    [titlesView addSubview:indicatorView];
    
}
//点击标签导航栏事件
- (void)titleClick:(UIButton *)button
{
    //修改按钮状态
    self.selectButton.enabled = YES;
    button.enabled = NO;
    self.selectButton = button;
    // animation
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
        
    }];
    // scroll
    CGPoint offset = self.contentView.contentOffset;
    // 索引  button.tag
    offset.x = button.tag * self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
}


- (void)setUpContentView
{
   //不要自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *contentView = [[UIScrollView alloc] init];
    contentView.frame = self.view.bounds;
    // 滚完 再 加载数据  就要设置代理
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    [self.view insertSubview:contentView atIndex:0];
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
    self.contentView = contentView;
    //添加第一个控制器的view
    [self scrollViewDidEndScrollingAnimation:contentView];
    
    
    // 设置 内边距
//    CGFloat bottom = self.tabBarController.tabBar.height;
//    CGFloat top = CGRectGetMaxX(self.titleView.frame);
//    contentView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
//    [self.view insertSubview:contentView atIndex:0];
    //[contentView addSubview:[UIButton buttonWithType:UIButtonTypeContactAdd]];
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // content index
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    // 取出子控制器
    UITableViewController *vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    
    // 设置 控制器的 y ＝ 0   就不会下移了
    vc.view.y = 0;
    
    // tabelview 右边 滚动条的 内边距
    vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
    // 设置 scrollview 的高度  默认是比 屏幕高度  少个20（运营商那块的 高度是20）
    vc.tableView.height = scrollView.height;
    
    // 设置内边距
   
    CGFloat bottom = self.tabBarController.tabBar.height;
    CGFloat top = CGRectGetMaxY(self.titleView.frame);
    vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    [scrollView addSubview:vc.view];
}
// 停止减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    // button click
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self titleClick:self.titleView.subviews[index]];
}


@end
